package com.ruoyi.manage.service.impl;

import java.util.List;

import cn.hutool.core.text.StrJoiner;
import com.ruoyi.manage.domain.ActivityUser;
import com.ruoyi.manage.domain.dto.UpdateActivityUserDto;
import com.ruoyi.manage.domain.vo.ActivityUserVo;
import com.ruoyi.manage.domain.vo.ActivityVo;
import com.ruoyi.manage.domain.vo.EnrollUser;
import com.ruoyi.manage.mapper.ActivityUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.ActivityMapper;
import com.ruoyi.manage.domain.Activity;
import com.ruoyi.manage.service.IActivityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动信息Service业务层处理
 * 
 * @author zzg
 * @date 2025-05-10
 */
@Service
public class ActivityServiceImpl implements IActivityService 
{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityUserMapper  activityUserMapper;

    /**
     * 查询活动信息
     * 
     * @param activityId 活动信息主键
     * @return 活动信息
     */
    @Override
    public Activity selectActivityByActivityId(Long activityId)
    {
        return activityMapper.selectActivityByActivityId(activityId);
    }

    /**
     * 根据活动id和用户id查询活动信息
     * @param activityUser
     * @return
     */
    public ActivityUser selectActivityByActivityUser(ActivityUser activityUser) {
        return activityUserMapper.selectActivityUserByActivityUser(activityUser);
    }

    /**
     * 查询活动信息列表
     * 
     * @param activity 活动信息
     * @return 活动信息
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        return activityMapper.selectActivityList(activity);
    }

    /**
     * 新增活动信息
     * 
     * @param activity 活动信息
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity)
    {
        return activityMapper.insertActivity(activity);
    }

    /**
     * 修改活动信息
     * 
     * @param activity 活动信息
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity) {
        //让活动上轮播图-轮播图上限10
        if(activity.getIsBanner() == 1){
            Activity activity1 = new Activity();
            activity1.setIsBanner(1);
            List<Activity> activityList = activityMapper.selectActivityList(activity1);
            if(activityList.size() >= 10){
                throw new RuntimeException("轮播图上限为10");
            }
        }
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除活动信息
     * 
     * @param activityIds 需要删除的活动信息主键
     * @return 结果
     */
    @Override
    public int deleteActivityByActivityIds(Long[] activityIds)
    {
        return activityMapper.deleteActivityByActivityIds(activityIds);
    }

    /**
     * 删除活动信息信息
     * 
     * @param activityId 活动信息主键
     * @return 结果
     */
    @Override
    public int deleteActivityByActivityId(Long activityId)
    {
        return activityMapper.deleteActivityByActivityId(activityId);
    }

    /**
     * 报名活动
     * @param activityUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int enroll(ActivityUser activityUser) {
        if (activityUserMapper.selectActivityUserByActivityUser(activityUser) != null){
            throw new RuntimeException("高活动已报名,不能重复报名");
        }
        Activity activity = activityMapper.selectActivityByActivityId(activityUser.getActivityId());
        if(activity.getCurrentParticipants() >= activity.getMaxParticipants()&&activity.getMaxParticipants()!=0){
            throw new RuntimeException("活动已满员,不能报名");
        }
        activity.setCurrentParticipants(activity.getCurrentParticipants() + 1);
        activityMapper.updateActivity(activity);
        return activityUserMapper.insertActivityUser(activityUser);
    }

    /**
     * 取消报名
     * @param activityUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer cancelEnroll(ActivityUser activityUser) {
        Integer integer = activityUserMapper.deleteActivityUser(activityUser);
        if(integer == 0){
            throw new RuntimeException("取消报名失败");
        }
        Activity activity = activityMapper.selectActivityByActivityId(activityUser.getActivityId());
        activity.setCurrentParticipants(activity.getCurrentParticipants() - 1);
        activityMapper.updateActivity(activity);
        return integer;
    }

    /**
     * 查询活动-用户列表
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<ActivityUserVo> selectActivityVoList(Long userId,Integer finish) {
        ActivityUser au = ActivityUser.builder().userId(userId).finish(finish).build();
        List<ActivityUserVo> activityUserVos = activityUserMapper.selectActivityUserVoList(au);
        return activityUserVos;
    }

    /**
     * 根据活动id查询报名用户列表
     * @param activityId
     * @return
     */
    public List<EnrollUser> selectEnrollUserListByActivityId(Long activityId) {
        return activityUserMapper.selectEnrollUserListByActivityId(activityId);
    }

    /**
     * 批量修改用户的活动状态
     * @param updateActivityUserDto
     * @return
     */
    public int updateBatchByActivityIdAndUserIds(UpdateActivityUserDto updateActivityUserDto) {
        return activityUserMapper.updateBatchByActivityIdAndUserIds(updateActivityUserDto);
    }
}
